#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_mipi_dsi.h"
#include "esp_lcd_st7701.h"
#include "esp_ldo_regulator.h"
#include "driver/gpio.h"
#include "driver/ledc.h"
#include "driver/i2c_master.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_heap_caps.h"
#include "esp_lcd_touch.h"
#include "esp_lcd_touch_cst826.h"
#include "lvgl.h"
#include "lv_demos.h"

static const char *TAG = "st7701_mipi";

// LCD 分辨率配置
#define EXAMPLE_MIPI_DSI_LCD_H_RES      480
#define EXAMPLE_MIPI_DSI_LCD_V_RES      480

// 颜色格式配置
#if LV_COLOR_DEPTH == 16
#define MIPI_DPI_PX_FORMAT              (LCD_COLOR_PIXEL_FORMAT_RGB565)
#define BSP_LCD_COLOR_DEPTH             (16)
#elif LV_COLOR_DEPTH >= 24
#define MIPI_DPI_PX_FORMAT              (LCD_COLOR_PIXEL_FORMAT_RGB888)
#define BSP_LCD_COLOR_DEPTH             (24)
#endif

// PWM背光控制配置
#define LEDC_TIMER                      LEDC_TIMER_0
#define LEDC_MODE                       LEDC_LOW_SPEED_MODE
#define LEDC_OUTPUT_IO                  29    // 背光PWM输出GPIO
#define LEDC_CHANNEL                    LEDC_CHANNEL_0
#define LEDC_DUTY_RES                   LEDC_TIMER_13_BIT
#define LEDC_FREQUENCY                  (5000)
#define LEDC_DUTY_MAX                   (8191)  // 100% (2^13-1)
#define LEDC_DUTY_MIN                   (0)     // 0%

// GPIO引脚配置
#define EXAMPLE_PIN_NUM_BK_LIGHT        29
#define EXAMPLE_PIN_NUM_LCD_RST         24
#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL   1
#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL  0

// MIPI DSI PHY电源配置
#define EXAMPLE_MIPI_DSI_PHY_PWR_LDO_CHAN       3
#define EXAMPLE_MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV 2500

// 触摸屏配置
#define TOUCH_I2C_SDA_GPIO              26
#define TOUCH_I2C_SCL_GPIO              27
#define TOUCH_RST_GPIO                  28
#define TOUCH_INT_GPIO                  25
#define TOUCH_I2C_CLK_SPEED             100000

// LVGL配置
#define EXAMPLE_LVGL_DRAW_BUF_LINES     100     // 减少缓冲区行数以节省内存
#define EXAMPLE_LVGL_TICK_PERIOD_MS     2
#define EXAMPLE_LVGL_TASK_MAX_DELAY_MS  500
#define EXAMPLE_LVGL_TASK_MIN_DELAY_MS  1
#define EXAMPLE_LVGL_TASK_STACK_SIZE    (8 * 1024)  // 增加堆栈大小到8KB
#define EXAMPLE_LVGL_TASK_PRIORITY      2

// 全局变量
static SemaphoreHandle_t lvgl_api_mux = NULL;

// ST7701 MIPI初始化命令序列
static const st7701_lcd_init_cmd_t lcd_init_cmds[] = {
    {0xFF, (uint8_t[]){0x77, 0x01, 0x00, 0x00, 0x10}, 5, 0},
    {0xC0, (uint8_t[]){0x3B, 0x00}, 2, 0},
    {0xC1, (uint8_t[]){0x0B, 0x02}, 2, 0},
    {0xC2, (uint8_t[]){0x00, 0x02}, 2, 0},
    {0xCC, (uint8_t[]){0x10}, 1, 0},
    {0xCD, (uint8_t[]){0x08}, 1, 0},
    {0xB0, (uint8_t[]){0x02, 0x13, 0x1B, 0x0D, 0x10, 0x05, 0x08, 0x07, 0x07, 0x24, 0x04, 0x11, 0x0E, 0x2C, 0x33, 0x1D}, 16, 0},
    {0xB1, (uint8_t[]){0x05, 0x13, 0x1B, 0x0D, 0x11, 0x05, 0x08, 0x07, 0x07, 0x24, 0x04, 0x11, 0x0E, 0x2C, 0x33, 0x1D}, 16, 0},
    {0xFF, (uint8_t[]){0x77, 0x01, 0x00, 0x00, 0x11}, 5, 0},
    {0xB0, (uint8_t[]){0x5D}, 1, 0},
    {0xB1, (uint8_t[]){0x43}, 1, 0},
    {0xB2, (uint8_t[]){0x81}, 1, 0},
    {0xB3, (uint8_t[]){0x80}, 1, 0},
    {0xB5, (uint8_t[]){0x43}, 1, 0},
    {0xB7, (uint8_t[]){0x85}, 1, 0},
    {0xB8, (uint8_t[]){0x20}, 1, 0},
    {0xC1, (uint8_t[]){0x78}, 1, 0},
    {0xC2, (uint8_t[]){0x78}, 1, 0},
    {0xD0, (uint8_t[]){0x88}, 1, 0},
    {0xE0, (uint8_t[]){0x00, 0x00, 0x02}, 3, 0},
    {0xE1, (uint8_t[]){0x03, 0xA0, 0x00, 0x00, 0x04, 0xA0, 0x00, 0x00, 0x00, 0x20, 0x20}, 11, 0},
    {0xE2, (uint8_t[]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 13, 0},
    {0xE3, (uint8_t[]){0x00, 0x00, 0x11, 0x00}, 4, 0},
    {0xE4, (uint8_t[]){0x22, 0x00}, 2, 0},
    {0xE5, (uint8_t[]){0x05, 0xEC, 0xA0, 0xA0, 0x07, 0xEE, 0xA0, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16, 0},
    {0xE6, (uint8_t[]){0x00, 0x00, 0x11, 0x00}, 4, 0},
    {0xE7, (uint8_t[]){0x22, 0x00}, 2, 0},
    {0xE8, (uint8_t[]){0x06, 0xED, 0xA0, 0xA0, 0x08, 0xEF, 0xA0, 0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16, 0},
    {0xEB, (uint8_t[]){0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00}, 7, 0},
    {0xED, (uint8_t[]){0xFF, 0xFF, 0xFF, 0xBA, 0x0A, 0xBF, 0x45, 0xFF, 0xFF, 0x54, 0xFB, 0xA0, 0xAB, 0xFF, 0xFF, 0xFF}, 16, 0},
    {0xEF, (uint8_t[]){0x10, 0x0D, 0x04, 0x08, 0x3F, 0x1F}, 6, 0},
    {0xFF, (uint8_t[]){0x77, 0x01, 0x00, 0x00, 0x13}, 5, 0},
    {0xEF, (uint8_t[]){0x08}, 1, 0},
    {0xFF, (uint8_t[]){0x77, 0x01, 0x00, 0x00, 0x00}, 5, 0},
    {0x36, (uint8_t[]){0x00}, 1, 0},
    {0x3A, (uint8_t[]){0x55}, 1, 0},  // RGB565格式
    {0x11, (uint8_t[]){0x00}, 0, 120}, // 退出睡眠模式
    {0x29, (uint8_t[]){0x00}, 0, 20},  // 显示开启
};

// LVGL刷新回调函数
static void example_lvgl_flush_cb(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map)
{
    esp_lcd_panel_handle_t panel_handle = lv_display_get_user_data(disp);
    int offsetx1 = area->x1;
    int offsetx2 = area->x2;
    int offsety1 = area->y1;
    int offsety2 = area->y2;
    
    // 将绘制缓冲区传递给驱动程序
    esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, px_map);
}

// LVGL定时器中断
static void example_increase_lvgl_tick(void *arg)
{
    lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}

// LVGL API锁
static bool example_lvgl_lock(int timeout_ms)
{
    const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
    return xSemaphoreTakeRecursive(lvgl_api_mux, timeout_ticks) == pdTRUE;
}

// LVGL API解锁
static void example_lvgl_unlock(void)
{
    xSemaphoreGiveRecursive(lvgl_api_mux);
}

// LVGL任务处理
static void example_lvgl_port_task(void *arg)
{
    ESP_LOGI(TAG, "启动LVGL任务");
    uint32_t task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
    
    while (1) {
        if (example_lvgl_lock(-1)) {
            task_delay_ms = lv_timer_handler();
            example_lvgl_unlock();
        }
        
        // 限制任务延时范围
        if (task_delay_ms > EXAMPLE_LVGL_TASK_MAX_DELAY_MS) {
            task_delay_ms = EXAMPLE_LVGL_TASK_MAX_DELAY_MS;
        } else if (task_delay_ms < EXAMPLE_LVGL_TASK_MIN_DELAY_MS) {
            task_delay_ms = EXAMPLE_LVGL_TASK_MIN_DELAY_MS;
        }
        
        vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
    }
}

// DPI面板刷新完成通知
static bool example_notify_lvgl_flush_ready(esp_lcd_panel_handle_t panel, esp_lcd_dpi_panel_event_data_t *edata, void *user_ctx)
{
    lv_display_t *disp = (lv_display_t *)user_ctx;
    lv_display_flush_ready(disp);
    return false;
}

// 启用MIPI DSI PHY电源
static void example_bsp_enable_dsi_phy_power(void)
{
    esp_ldo_channel_handle_t ldo_mipi_phy = NULL;
    esp_ldo_channel_config_t ldo_mipi_phy_config = {
        .chan_id = EXAMPLE_MIPI_DSI_PHY_PWR_LDO_CHAN,
        .voltage_mv = EXAMPLE_MIPI_DSI_PHY_PWR_LDO_VOLTAGE_MV,
    };
    ESP_ERROR_CHECK(esp_ldo_acquire_channel(&ldo_mipi_phy_config, &ldo_mipi_phy));
    ESP_LOGI(TAG, "MIPI DSI PHY电源已启用");
}

// 初始化背光PWM控制
static void example_bsp_init_lcd_backlight(void)
{
    // 配置LEDC定时器
    ledc_timer_config_t ledc_timer = {
        .speed_mode       = LEDC_MODE,
        .timer_num        = LEDC_TIMER,
        .duty_resolution  = LEDC_DUTY_RES,
        .freq_hz         = LEDC_FREQUENCY,
        .clk_cfg         = LEDC_AUTO_CLK
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
    
    // 配置LEDC通道
    ledc_channel_config_t ledc_channel = {
        .speed_mode     = LEDC_MODE,
        .channel        = LEDC_CHANNEL,
        .timer_sel      = LEDC_TIMER,
        .intr_type      = LEDC_INTR_DISABLE,
        .gpio_num       = LEDC_OUTPUT_IO,
        .duty           = 0,  // 初始为关闭
        .hpoint        = 0
    };
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
    
    ESP_LOGI(TAG, "背光PWM初始化完成");
}

// 设置背光亮度
static void example_bsp_set_lcd_backlight(uint32_t level)
{
    uint32_t duty = level ? LEDC_DUTY_MAX : LEDC_DUTY_MIN;
    ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, duty));
    ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
}

// LVGL触摸读取回调函数
static void touchpad_read(lv_indev_t *indev, lv_indev_data_t *data)
{
    esp_lcd_touch_handle_t touch_handle = (esp_lcd_touch_handle_t)lv_indev_get_user_data(indev);
    
    if (touch_handle == NULL) {
        data->state = LV_INDEV_STATE_RELEASED;
        return;
    }
    
    uint16_t touchpad_x = 0;
    uint16_t touchpad_y = 0;
    uint8_t touchpad_cnt = 0;
    
    // 读取触摸数据
    esp_err_t ret = esp_lcd_touch_read_data(touch_handle);
    if (ret == ESP_OK) {
        bool touchpad_pressed = esp_lcd_touch_get_coordinates(touch_handle, &touchpad_x, &touchpad_y, NULL, &touchpad_cnt, 1);
        
        if (touchpad_pressed && touchpad_cnt > 0) {
            data->point.x = touchpad_x;
            data->point.y = touchpad_y;
            data->state = LV_INDEV_STATE_PRESSED;
        } else {
            data->state = LV_INDEV_STATE_RELEASED;
        }
    } else {
        data->state = LV_INDEV_STATE_RELEASED;
    }
}

// 初始化CST826触摸屏
static esp_lcd_touch_handle_t init_touch_cst826(void)
{
    esp_lcd_touch_handle_t touch_handle = NULL;
    
    // 初始化I2C主机
    i2c_master_bus_handle_t i2c_bus_handle = NULL;
    const i2c_master_bus_config_t bus_config = {
        .i2c_port = I2C_NUM_0,
        .sda_io_num = TOUCH_I2C_SDA_GPIO,
        .scl_io_num = TOUCH_I2C_SCL_GPIO,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = true,
    };
    
    esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "I2C主机初始化失败: %s", esp_err_to_name(ret));
        return NULL;
    }

    // 创建触摸Panel IO
    esp_lcd_panel_io_handle_t tp_io_handle = NULL;
    const esp_lcd_panel_io_i2c_config_t tp_io_config = {
        .dev_addr = 0x15,  // CST826 I2C地址
        .scl_speed_hz = TOUCH_I2C_CLK_SPEED,
        .control_phase_bytes = 1,
        .dc_bit_offset = 0,
        .lcd_cmd_bits = 8,
        .flags = {
            .disable_control_phase = 1,
        }
    };
    
    ret = esp_lcd_new_panel_io_i2c(i2c_bus_handle, &tp_io_config, &tp_io_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "触摸Panel IO创建失败: %s", esp_err_to_name(ret));
        return NULL;
    }

    // 创建触摸设备配置
    const esp_lcd_touch_config_t tp_cfg = {
        .x_max = EXAMPLE_MIPI_DSI_LCD_H_RES,
        .y_max = EXAMPLE_MIPI_DSI_LCD_V_RES,
        .rst_gpio_num = TOUCH_RST_GPIO,
        .int_gpio_num = TOUCH_INT_GPIO,
        .levels = {
            .reset = 0,
            .interrupt = 0,
        },
        .flags = {
            .swap_xy = 0,
            .mirror_x = 0,
            .mirror_y = 0,
        },
    };

    // 创建CST826触摸驱动
    ret = esp_lcd_touch_new_i2c_cst826(tp_io_handle, &tp_cfg, &touch_handle);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "CST826触摸驱动创建失败: %s", esp_err_to_name(ret));
        return NULL;
    }
    
    ESP_LOGI(TAG, "CST826触摸屏初始化完成");
    return touch_handle;
}

void app_main(void)
{
    ESP_LOGI(TAG, "ESP32-P4 ST7701 MIPI DSI LCD演示程序启动");
    
    // 1. 启用MIPI DSI PHY电源
    example_bsp_enable_dsi_phy_power();
    
    // 2. 初始化背光（先关闭）
    example_bsp_init_lcd_backlight();
    example_bsp_set_lcd_backlight(EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
    
    // 3. 创建MIPI DSI总线
    esp_lcd_dsi_bus_handle_t mipi_dsi_bus;
    esp_lcd_dsi_bus_config_t bus_config = {
        .bus_id = 0,
        .num_data_lanes = 2,
        .phy_clk_src = MIPI_DSI_PHY_CLK_SRC_DEFAULT,
        .lane_bit_rate_mbps = 400,  // 降低位速率提高稳定性
    };
    ESP_ERROR_CHECK(esp_lcd_new_dsi_bus(&bus_config, &mipi_dsi_bus));
    ESP_LOGI(TAG, "MIPI DSI总线创建完成");
    
    // 4. 创建MIPI DBI IO
    esp_lcd_panel_io_handle_t mipi_dbi_io;
    esp_lcd_dbi_io_config_t dbi_config = ST7701_PANEL_IO_DBI_CONFIG();
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_dbi(mipi_dsi_bus, &dbi_config, &mipi_dbi_io));
    ESP_LOGI(TAG, "MIPI DBI IO创建完成");
    
    // 5. 创建ST7701控制面板
    esp_lcd_panel_handle_t panel_handle;
    esp_lcd_dpi_panel_config_t dpi_config = {
        .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT,
        .dpi_clock_freq_mhz = 20,  // 降低时钟频率提高稳定性
        .virtual_channel = 0,
        .pixel_format = MIPI_DPI_PX_FORMAT,
        .num_fbs = 1,
        .video_timing = {
            .h_size = EXAMPLE_MIPI_DSI_LCD_H_RES,
            .v_size = EXAMPLE_MIPI_DSI_LCD_V_RES,
            .hsync_back_porch = 40,
            .hsync_pulse_width = 20,
            .hsync_front_porch = 40,
            .vsync_back_porch = 30,
            .vsync_pulse_width = 20,
            .vsync_front_porch = 30,
        },
        .flags.use_dma2d = true,  // 启用DMA2D提高性能
    };
    
    st7701_vendor_config_t vendor_config = {
        .init_cmds = lcd_init_cmds,
        .init_cmds_size = sizeof(lcd_init_cmds) / sizeof(st7701_lcd_init_cmd_t),
        .flags.use_mipi_interface = 1,
        .mipi_config = {
            .dsi_bus = mipi_dsi_bus,
            .dpi_config = &dpi_config,
        },
    };
    
    const esp_lcd_panel_dev_config_t panel_config = {
        .reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST,
        .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
        .bits_per_pixel = BSP_LCD_COLOR_DEPTH,
        .vendor_config = &vendor_config,
    };
    
    ESP_ERROR_CHECK(esp_lcd_new_panel_st7701(mipi_dbi_io, &panel_config, &panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
    ESP_LOGI(TAG, "ST7701面板初始化完成");
    
    // 6. 开启背光
    example_bsp_set_lcd_backlight(EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
    
    // 7. 初始化LVGL
    ESP_LOGI(TAG, "初始化LVGL库");
    lv_init();
    
    // 8. 创建LVGL显示
    lv_display_t *display = lv_display_create(EXAMPLE_MIPI_DSI_LCD_H_RES, EXAMPLE_MIPI_DSI_LCD_V_RES);
    lv_display_set_user_data(display, panel_handle);
    
    // 9. 分配绘制缓冲区
    ESP_LOGI(TAG, "分配LVGL绘制缓冲区");
    size_t draw_buffer_sz = EXAMPLE_MIPI_DSI_LCD_H_RES * EXAMPLE_LVGL_DRAW_BUF_LINES * sizeof(lv_color_t);
    void *buf1 = heap_caps_malloc(draw_buffer_sz, MALLOC_CAP_SPIRAM);
    void *buf2 = heap_caps_malloc(draw_buffer_sz, MALLOC_CAP_SPIRAM);
    
    if (!buf1 || !buf2) {
        ESP_LOGE(TAG, "绘制缓冲区分配失败");
        return;
    }
    
    // 10. 初始化LVGL绘制缓冲区
    lv_display_set_buffers(display, buf1, buf2, draw_buffer_sz, LV_DISPLAY_RENDER_MODE_PARTIAL);
    
#if LV_COLOR_DEPTH == 16
    lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB565);
#else
    lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB888);
#endif
    
    lv_display_set_flush_cb(display, example_lvgl_flush_cb);
    
    // 11. 注册DPI面板事件回调
    ESP_LOGI(TAG, "注册DPI面板事件回调");
    esp_lcd_dpi_panel_event_callbacks_t cbs = {
        .on_color_trans_done = example_notify_lvgl_flush_ready,
    };
    ESP_ERROR_CHECK(esp_lcd_dpi_panel_register_event_callbacks(panel_handle, &cbs, display));
    
    // 12. 设置LVGL定时器
    ESP_LOGI(TAG, "配置LVGL定时器");
    const esp_timer_create_args_t lvgl_tick_timer_args = {
        .callback = &example_increase_lvgl_tick,
        .name = "lvgl_tick"
    };
    esp_timer_handle_t lvgl_tick_timer = NULL;
    ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
    ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
    
    // 13. 创建LVGL API互斥锁
    lvgl_api_mux = xSemaphoreCreateRecursiveMutex();
    if (!lvgl_api_mux) {
        ESP_LOGE(TAG, "LVGL互斥锁创建失败");
        return;
    }
    
    // 14. 创建LVGL任务
    ESP_LOGI(TAG, "创建LVGL任务");
    BaseType_t ret = xTaskCreate(
        example_lvgl_port_task, 
        "LVGL", 
        EXAMPLE_LVGL_TASK_STACK_SIZE, 
        NULL, 
        EXAMPLE_LVGL_TASK_PRIORITY, 
        NULL
    );
    
    if (ret != pdPASS) {
        ESP_LOGE(TAG, "LVGL任务创建失败");
        return;
    }
    
    // 15. 初始化触摸屏并添加到LVGL
    esp_lcd_touch_handle_t touch_handle = init_touch_cst826();
    lv_indev_t *touch_indev = NULL;
    if (touch_handle) {
        // 创建LVGL触摸输入设备
        lv_indev_t * indev = lv_indev_create();
        lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
        lv_indev_set_read_cb(indev, touchpad_read);
        lv_indev_set_user_data(indev, touch_handle);
        touch_indev = indev;
        
        ESP_LOGI(TAG, "LVGL触摸输入设备已添加");
    } else {
        ESP_LOGW(TAG, "触摸屏初始化失败，继续运行");
    }
    
    // 16. 显示演示UI（延时启动避免堆栈压力）
    ESP_LOGI(TAG, "系统初始化完成，进入主循环");
    
    // 主循环 - 定期打印系统状态
    bool demo_started = false;
    while (1) {
        // 延时1秒后启动演示程序，确保系统完全稳定
        if (!demo_started) {
            vTaskDelay(pdMS_TO_TICKS(1000));
            ESP_LOGI(TAG, "启动LVGL控件演示");
            if (example_lvgl_lock(1000)) {
                lv_demo_widgets();
                example_lvgl_unlock();
                demo_started = true;
                ESP_LOGI(TAG, "LVGL控件演示已启动");
                if (touch_indev) {
                    ESP_LOGI(TAG, "触摸功能已启用");
                }
            }
        }
        
        vTaskDelay(pdMS_TO_TICKS(10000));  // 10秒间隔
        ESP_LOGI(TAG, "系统运行正常 - 可用堆内存: %lu 字节", 
                 (unsigned long)esp_get_free_heap_size());
    }
}
